<?php

declare(strict_types=1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;
use think\facade\Db;

class TrandingbankListsr extends Model
{
    use SoftDelete;
    protected $deleteTime = "delete_time";
    // 获取列表
    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');

        //按出售物品玩家账号查找
        if ($sellplayaccount = input("sellplayaccount")) {
            $where[] = ["sellplayaccount", "like", "%" . $sellplayaccount . "%"];
        }
        //按出售物品玩家actorid查找
        if ($shellplayactorid = input("shellplayactorid")) {
            $where[] = ["shellplayactorid", "like", "%" . $shellplayactorid . "%"];
        }
        //按出售物品玩家昵称查找
        if ($shellplaynikename = input("shellplaynikename")) {
            $where[] = ["shellplaynikename", "like", "%" . $shellplaynikename . "%"];
        }
        //按出售物品玩家区服查找
        if ($shellplayserverid = input("shellplayserverid")) {
            $where[] = ["shellplayserverid", "like", "%" . $shellplayserverid . "%"];
        }
        //按出售物品玩家上架时间查找
        $start = input("get.shellplaygrotime-start");
        $end = input("get.shellplaygrotime-end");
        if ($start && $end) {
            $where[] = ["shellplaygrotime", "between", [$start, date("Y-m-d", strtotime("$end +1 day"))]];
        }
        //按出售物品id查找
        if ($shellgoodsid = input("shellgoodsid")) {
            $where[] = ["shellgoodsid", "like", "%" . $shellgoodsid . "%"];
        }
        //按出售物品名称查找
        if ($shellgoodsname = input("shellgoodsname")) {
            $where[] = ["shellgoodsname", "like", "%" . $shellgoodsname . "%"];
        }
        //按出售物品数量查找
        if ($shellgoodsnum = input("shellgoodsnum")) {
            $where[] = ["shellgoodsnum", "like", "%" . $shellgoodsnum . "%"];
        }
        //按出售物品价格查找
        if ($shellgoodsprice = input("shellgoodsprice")) {
            $where[] = ["shellgoodsprice", "like", "%" . $shellgoodsprice . "%"];
        }
        //按出售物品手续费查找
        if ($goodshandingfees = input("goodshandingfees")) {
            $where[] = ["goodshandingfees", "like", "%" . $goodshandingfees . "%"];
        }
        //按出售物品状态 1.在售 2.已售查找
        if ($goodstatus = input("goodstatus")) {
            $where[] = ["goodstatus", "like", "%" . $goodstatus . "%"];
        }
        //按购买玩家账号查找
        if ($buyplayaccount = input("buyplayaccount")) {
            $where[] = ["buyplayaccount", "like", "%" . $buyplayaccount . "%"];
        }
        //按购买玩家actorid查找
        if ($buyplayactorid = input("buyplayactorid")) {
            $where[] = ["buyplayactorid", "like", "%" . $buyplayactorid . "%"];
        }
        //按购买玩家昵称查找
        if ($buyplaynikename = input("buyplaynikename")) {
            $where[] = ["buyplaynikename", "like", "%" . $buyplaynikename . "%"];
        }
        //按购买玩家区服查找
        if ($buyplayserverid = input("buyplayserverid")) {
            $where[] = ["buyplayserverid", "like", "%" . $buyplayserverid . "%"];
        }
        //按购买玩家的购买价格查找
        if ($buyplaygoodsprice = input("buyplaygoodsprice")) {
            $where[] = ["buyplaygoodsprice", "like", "%" . $buyplaygoodsprice . "%"];
        }
        //按玩家购买时间查找
        $start = input("get.buyplaygortime-start");
        $end = input("get.buyplaygortime-end");
        if ($start && $end) {
            $where[] = ["buyplaygortime", "between", [$start, date("Y-m-d", strtotime("$end +1 day"))]];
        }
        $list = self::order('id', 'desc')->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit]];
    }


    public static function getShellList($actorid, $serverid)
    {
        $where = [];
        $limit = input('get.limit');

        $where[] = ["goodstatus", "=", 1];
        $where[] = ["shellplayactorid", "=", $actorid];
        $where[] = ["shellplayserverid", "=", $serverid];

        $list = self::order('id', 'desc')->field('id,shellgoodsid,shellgoodsname,shellgoodsnum,shellgoodsprice,goodshandingfees,shellplaygrotime')
            ->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'count' => $list->total(), 'msg' => ''];
    }

    public static function getbuylList($actorid, $serverid)
    {
        $where = [];
        $limit = input('get.limit');

        if ($shellgoodsname = input("shellgoodsname")) {
            $where[] = ["shellgoodsname", "like", "%" . $shellgoodsname . "%"];
        }

        $where[] = ["goodstatus", "=", 1];

        // 查询 trandingbank_kfconfig 表，获取符合条件的 statrid 和 endid
        $config = Db::table('trandingbank_kfconfig')
            ->where('statrid', '<=', $serverid)
            ->where('endid', '>=', $serverid)
            ->find();

        // 如果没有找到符合条件的记录，直接返回空数据
        if (!$config) {
            return ['code' => 0, 'data' => [], 'count' => 0, 'msg' => ''];
        }

        // 设置 where 条件为 statrid 和 endid
        $where[] = ['shellplayserverid', '>=', $config['statrid']];
        $where[] = ['shellplayserverid', '<=', $config['endid']];


        $list = self::order('id', 'desc')
            ->field('id,shellgoodsid,shellgoodsname,shellgoodsnum,shellgoodsprice,shellplaygrotime')
            ->where($where)
            ->paginate($limit);

        return ['code' => 0, 'data' => $list->items(), 'count' => $list->total(), 'msg' => ''];
    }

    public static function getmyShellList($actorid, $serverid)
    {
        $where = [];
        $limit = input('get.limit');

        $where[] = ["shellplayactorid", "=", $actorid];
        $where[] = ["shellplayserverid", "=", $serverid];

        $list = self::order('shellplaygrotime', 'desc')
            ->field('id,shellgoodsname,shellgoodsnum,shellgoodsprice,goodshandingfees,goodstatus,buyplaynikename,buyplayserverid,buyplaygortime')
            ->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'count' => $list->total(), 'msg' => ''];
    }

    public static function getmybuylList($actorid, $serverid)
    {
        $where = [];
        $limit = input('get.limit');

        $where[] = ["goodstatus", "=", 2];
        $where[] = ["buyplayactorid", "=", $actorid];
        $where[] = ["buyplayserverid", "=", $serverid];

        $list = self::order('id', 'desc')->field('id,shellgoodsname,shellgoodsnum,shellgoodsprice,shellplaynikename,shellplayserverid,buyplaygortime')
            ->where($where)->paginate($limit);
        return ['code' => 0, 'data' => $list->items(), 'count' => $list->total(), 'msg' => ''];
    }

    //获取物品列表
    public static function getGoodsls()
    {
        $sql = "SELECT * FROM trandingbank_allowgoods;";
        $result = Db::query($sql);
        if (!empty($result)) {
            return $result;
        } else {
            return [];
        }
    }

    //验证状态
    public static function checkStatus($id)
    {
        $sql = "SELECT * FROM trandingbank_listsr WHERE id = :id";
        $params = array('id' => $id);
        $result = Db::query($sql, $params);

        if (!empty($result)) {
            return $result[0];
        } else {
            return 0;
        }
    }

    //修改状态
    public static function moStatus($id)
    {
        $sql = "update trandingbank_listsr set goodstatus=3 where id='$id';";
        Db::execute($sql);
    }


    //邮件发送
    public static function sendMail($serverid, $actorid, $names, $idstr)
    {
        $newserverid = self::getDBName($serverid);
        $sql = "INSERT INTO $newserverid.gmcmd (serverid,cmdid,cmd,param1,param2,param3,param4,param5) 
                 VALUES ('$serverid','1','sendMail','交易行物品下架退还', '$names','$actorid','$idstr','');";
        Db::execute($sql);
    }

    //邮件发送
    public static function sendMailBuy($serverid, $actorid, $names, $idstr)
    {
        $newserverid = self::getDBName($serverid);
        $sql = "INSERT INTO $newserverid.gmcmd (serverid,cmdid,cmd,param1,param2,param3,param4,param5) 
                 VALUES ('$serverid','1','sendMail','交易行物品出售成功！', '$names','$actorid','$idstr','');";
        Db::execute($sql);
    }


    //获取玩家信息
    public static function getPlayerInfo($serverid, $actorid)
    {
        $newserverid = self::getDBName($serverid);


        $sql = "select * from $newserverid.`actors` where actorid='$actorid';";
        return Db::query($sql)[0];
    }


    // 获取数据库名称 传入$serverid
    public static function getDBName($serverid)
    {
        $newserverid = 'h5_cq_';
        // 使用正则表达式匹配并获取有效的字符
        $integerValue = intval($serverid) - 1000;
        // 如果有匹配到有效字符，则返回匹配结果，否则返回 "0"
        $trimmedString = $integerValue < 0 ? "0" : strval($integerValue);
        if (env('APP_DEBUG')) {
            $newserverid .= '00' . $trimmedString;
        } else {
            $newserverid .= '' . $trimmedString;
        }

        return $newserverid;
    }
}
